package com.powernode.ex.handler;

import com.powernode.constant.BusinessEnum;
import com.powernode.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @Author 梁卓
 * @Version 1.0
 * @ClassName GlobalExceptionHandle
 * @Description 全局异常处理类
 * @Date 2024/07/21 14:44
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandle {

    /**
     * 处理：业务异常
     * @param e 异常
     * @return 返回结果
     */
    @ExceptionHandler(BusinessException.class)
    public Result<String> runtimeException(BusinessException e){
        log.error("业务异常:{}",e.getMessage());
        return Result.fail(BusinessEnum.OPERATION_FAIL.getCode(), e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public Result<String> runtimeException(RuntimeException e){
        log.error("运行时异常:{}",e.getMessage());
        return Result.fail(BusinessEnum.SERVER_INNER_ERROR);
    }

    /**
     * 处理：权限不足异常
     * @param e 异常
     * @throws AccessDeniedException 权限不足异常
     */
    @ExceptionHandler(AccessDeniedException.class)
    public void accessDeniedException(AccessDeniedException e) throws AccessDeniedException {
        // 抛出AccessDeniedException异常，走自定义AccessDeniedException异常处理
        log.error("权限不足异常:{}",e.getMessage());
        throw e;
    }

}
